package  com.sat.common.utils

import android.app.Activity
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Rect
import android.util.DisplayMetrics
import android.view.inputmethod.InputMethodManager
import  com.sat.common.base.BaseAPP
import java.lang.reflect.Field


object ScreenUtil {
    /**
     * 屏幕宽高
     *
     * @param context
     * @return
     */
    fun getScreenSize(context: Context): IntArray {
        var dm = DisplayMetrics()
        dm = context.resources.displayMetrics
        val screenWidth = dm.widthPixels
        val screenHeight = dm.heightPixels
        return intArrayOf(screenWidth, screenHeight)
    }

    /**
     * @param context
     * @return int
     * @Title: getStatusBarHeight
     * @Description: 获取状态栏高度
     */
    fun getStatusBarHeight(context: Context): Int {
        var c: Class<*>? = null
        var obj: Any? = null
        var field: Field? = null
        var x = 0
        var statusBarHeight = 0
        try {
            c = Class.forName("com.android.internal.R\$dimen")
            obj = c.newInstance()
            field = c.getField("status_bar_height")
            x = field[obj].toString().toInt()
            statusBarHeight = context.resources.getDimensionPixelSize(x)
        } catch (e1: Exception) {
            e1.printStackTrace()
        }
        return statusBarHeight
    }

    /**
     * @param @return 设定文件
     * @return int 返回类型
     * @throws
     * @Title: getScreenWidth
     * @Description: 获取手机屏幕的宽度
     */
    fun getScreenWidth(context: Context): Int {
        val screen = getScreenSize(context)
        return screen[0]
    }

    /**
     * @param @return 设定文件
     * @return int 返回类型
     * @throws
     * @Title: getScreenHeight
     * @Description: 获取手机屏幕的高度
     */
    fun getScreenHeight(context: Context): Int {
        val screen = getScreenSize(context)
        return screen[1]
    }

    /**
     * 根据手机分辨率将dp转为px单位
     */
    fun dip2px(dpValue: Float): Int {
        val scale: Float = BaseAPP.getContext().resources.displayMetrics.density
        return (dpValue * scale + 0.5f).toInt()
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     */
    fun px2dip(pxValue: Float): Int {
        val scale: Float = BaseAPP.getInstance().getResources().getDisplayMetrics().density
        return (pxValue / scale + 0.5f).toInt()
    }

    /**
     * 获得状态栏的高度
     *
     * @param context
     * @return
     */
    fun getStatusHeight(context: Context): Int {
        var statusHeight = -1
        try {
            val clazz = Class.forName("com.android.internal.R\$dimen")
            val `object` = clazz.newInstance()
            val height = clazz.getField("status_bar_height")[`object`].toString().toInt()
            statusHeight = context.resources.getDimensionPixelSize(height)
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return statusHeight
    }

    /**
     * 获取当前屏幕截图，包含状态栏
     *
     * @param activity
     * @return
     */
    fun snapShotWithStatusBar(activity: Activity): Bitmap? {
        val view = activity.window.decorView
        view.isDrawingCacheEnabled = true
        view.buildDrawingCache()
        val bmp = view.drawingCache
        val width = getScreenWidth(activity)
        val height = getScreenHeight(activity)
        var bp: Bitmap? = null
        bp = Bitmap.createBitmap(bmp, 0, 0, width, height)
        view.destroyDrawingCache()
        return bp
    }

    /**
     * 获取当前屏幕截图，不包含状态栏
     *
     * @param activity
     * @return
     */
    fun snapShotWithoutStatusBar(activity: Activity): Bitmap? {
        val view = activity.window.decorView
        view.isDrawingCacheEnabled = true
        view.buildDrawingCache()
        val bmp = view.drawingCache
        val frame = Rect()
        activity.window.decorView.getWindowVisibleDisplayFrame(frame)
        val statusBarHeight = frame.top
        val width = getScreenWidth(activity)
        val height = getScreenHeight(activity)
        var bp: Bitmap? = null
        bp = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height - statusBarHeight)
        view.destroyDrawingCache()
        return bp
    }

    /**
     * 屏幕宽高
     *
     * @param
     * @return
     */
    private val screenSize: IntArray
        private get() {
            var dm = DisplayMetrics()
            dm = BaseAPP.getInstance().applicationContext.resources.displayMetrics
            val screenWidth = dm.widthPixels
            val screenHeight = dm.heightPixels
            return intArrayOf(screenWidth, screenHeight)
        }

    /**
     * @param @return 设定文件
     * @return int 返回类型
     * @throws
     * @Title: getScreenWidth
     * @Description: 获取手机屏幕的宽度
     */
    val screenWidth: Int
        get() {
            val screen = screenSize
            return screen[0]
        }

    /**
     * @param @return 设定文件
     * @return int 返回类型
     * @throws
     * @Title: getScreenHeight
     * @Description: 获取手机屏幕的高度
     */
    val screenHeight: Int
        get() {
            val screen = screenSize
            return screen[1]
        }

    /**
     * @return
     * @author：liaoxy
     * @Description:是否要显示输入法 有bug
     */
    fun showSoftInput(isShow: Boolean) {
        val imm =
            BaseAPP.getInstance().applicationContext
                .getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        val isOpen = imm.isActive // isOpen若返回true，则表示输入法打开,坑爹的是一直为true
        if (isShow xor isOpen) { //如果不同则为真
            imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS)
        }
    }
}
